package com.lanmaoly.web.server.controller;

import com.alibaba.fastjson.JSON;
import com.lanmaoly.web.server.BaseController;
import com.lanmaoly.web.server.dao.TbGoodsDescMapper;
import com.lanmaoly.web.server.dao.TbGoodsMapper;
import com.lanmaoly.web.server.dao.TbItemCatMapper;
import com.lanmaoly.web.server.dao.TbItemMapper;
import com.lanmaoly.web.server.entity.Result;
import com.lanmaoly.web.server.pojo.*;
import com.lanmaoly.web.server.service.ItemSearchService;
import com.lanmaoly.web.server.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 首页
 *
 * @author hou 2018-11-30 10:57:17
 */
@Slf4j
@Controller
public class IndexController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping(value = {"/", "", "index"})
    public String index() {
        return "portal/search";
    }

    @RequestMapping(value = "authentication/require")
    public String require() {
        return "portal/login";
    }

    @RequestMapping(value = "register")
    public String register() {
        return "portal/register";
    }

    @ResponseBody
    @PostMapping(value = "register/registerd")
    public Result registerd(TbUser user) {
        if (StringUtils.isEmpty(user.getUsername())) {
            return Result.fail("请填写好用户名");
        }
        if (user.getPassword().length() < 6) {
            return Result.fail("密码不可少于六位");
        }
        userService.add(user);
        return Result.success("注册成功");
    }

    @RequestMapping(value = "forget")
    public String forget() {
        return "portal/forget";
    }

    @ResponseBody
    @RequestMapping(value = "forget/reset")
    public Result reset(TbUser user) {
        return userService.resetPassword(user);
    }

    /**
     * 商家入驻
     *
     * @return String
     */
    @RequestMapping(value = "settle")
    public String settle() {
        return "portal/settle";
    }

    /**
     * 商家入驻
     *
     * @return String
     */
    @PostMapping(value = "register/settle")
    @ResponseBody
    public Result registerSettle(@Validated SellerTemporaryEntity tbSeller) {
        log.info("商家入驻{}", tbSeller);
        userService.registerSettle(tbSeller);
        return Result.success("注册成功");
    }

    @Autowired
    private ItemSearchService itemSearchService;
    @Autowired
    private TbGoodsMapper tbGoodsMapper;
    @Autowired
    private TbGoodsDescMapper tbGoodsDescMapper;
    @Autowired
    private TbItemMapper tbItemMapper;
    @Autowired
    private TbItemCatMapper itemCatMapper;

    @RequestMapping("itemSearch")
    public String itemSearc(String keywords, Model model) {
        model.addAttribute("keywords", keywords);
        return "portal/search";
    }

    @ResponseBody
    @RequestMapping("itemSearch/search")
    public Map search(@RequestBody Map<String, Object> searchMap) {
        return itemSearchService.search(searchMap);
    }

    /**
     * 详情
     *
     * @param goodsId goodsId
     * @return Map
     */
    @RequestMapping("itemSearch/details")
    public String details(Long goodsId, Model model) {
        TbGoods goods = tbGoodsMapper.selectByPrimaryKey(goodsId);
        model.addAttribute("goods", goods);
        TbGoodsDesc goodsDesc = tbGoodsDescMapper.selectByPrimaryKey(goodsId);
        model.addAttribute("goodsDesc", goodsDesc);
        List<Map> imageList = JSON.parseArray(goodsDesc.getItemImages(), Map.class);
        model.addAttribute("imageList", imageList);
        List<Map> specificationList = JSON.parseArray(goodsDesc.getSpecificationItems(), Map.class);
        model.addAttribute("specificationList", specificationList);
        List<Map> customAttributeList = JSON.parseArray(goodsDesc.getCustomAttributeItems(), Map.class);
        model.addAttribute("customAttributeList", customAttributeList);
        String itemCat1 = itemCatMapper.selectByPrimaryKey(goods.getCategory1Id()).getName();
        String itemCat2 = itemCatMapper.selectByPrimaryKey(goods.getCategory2Id()).getName();
        String itemCat3 = itemCatMapper.selectByPrimaryKey(goods.getCategory3Id()).getName();
        model.addAttribute("itemCat1", itemCat1);
        model.addAttribute("itemCat2", itemCat2);
        model.addAttribute("itemCat3", itemCat3);
        String orderBy = "is_default desc";
        Map<String, Object> searchMap = new HashMap<>(4);
        searchMap.put("goodsId", goodsId);
        searchMap.put("orderBy", orderBy);
        List<TbItem> itemList = tbItemMapper.selectBySearchMap(searchMap);
        model.addAttribute("itemList", itemList);
        model.addAttribute("skuList", itemList);
        return "portal/item";
    }
}
